package com.koron.housing.apply.service.impl;


import com.koron.housing.apply.bean.po.HousingStaffIntegral;
import com.koron.housing.apply.mapper.HousingStaffIntegralMapper;
import com.koron.housing.apply.service.HousingStaffIntegralService;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 积分信息(HousingStaffIntegral)表服务实现类
 *
 * @author jzp
 * @since 2022-02-26 15:22:41
 */
@Service("integralService")
public class HousingStaffIntegralServiceImpl implements HousingStaffIntegralService {

    /**
     * 通过ID查询单条数据
     */
    @Override
    @TaskAnnotation("getById")
    public HousingStaffIntegral getById(SessionFactory factory, String id) {
        HousingStaffIntegralMapper mapper = factory.getMapper(HousingStaffIntegralMapper.class);
        return mapper.selectById(id);
    }

    @Override
    @TaskAnnotation("getOneByApplyId")
    public HousingStaffIntegral getOneByApplyId(SessionFactory factory, String applyId) {
        HousingStaffIntegralMapper mapper = factory.getMapper(HousingStaffIntegralMapper.class);
        return mapper.selectOneByApplyId(applyId);
    }

    /**
     * 查询所有数据
     */
    @Override
    @TaskAnnotation("list")
    public List<HousingStaffIntegral> list(SessionFactory factory, HousingStaffIntegral housingStaffIntegral) {
        HousingStaffIntegralMapper mapper = factory.getMapper(HousingStaffIntegralMapper.class);
        return mapper.selectList(housingStaffIntegral);
    }

    @Override
    @TaskAnnotation("listByApplyId")
    public List<HousingStaffIntegral> listByApplyId(SessionFactory factory, String applyId) {
        HousingStaffIntegral integral = new HousingStaffIntegral();
        integral.setApplyId(applyId);
        return list(factory, integral);
    }

    /**
     * 新增数据
     */
    @Override
    @TaskAnnotation("save")
    public boolean save(SessionFactory factory, HousingStaffIntegral housingStaffIntegral) {
        Assert.hasText(housingStaffIntegral.getApplyId(), "积分信息未关联申请单id");
        if (StringUtils.isEmpty(housingStaffIntegral.getId())) {
            HousingStaffIntegral source = getOneByApplyId(factory, housingStaffIntegral.getApplyId());
            if (source != null) {
                housingStaffIntegral.setId(source.getId());
            }
        }
        // 每次都重新计算计算积分信息
        Integer total = housingStaffIntegral.computeTotal();
        housingStaffIntegral.setTotal(total);
        HousingStaffIntegralMapper mapper = factory.getMapper(HousingStaffIntegralMapper.class);
        return mapper.insertOrUpdate(housingStaffIntegral) > 0;
    }

    /**
     * 修改数据
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, HousingStaffIntegral housingStaffIntegral) {
        HousingStaffIntegralMapper mapper = factory.getMapper(HousingStaffIntegralMapper.class);
        return mapper.update(housingStaffIntegral) > 0;
    }

    /**
     * 通过主键删除数据
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        HousingStaffIntegralMapper mapper = factory.getMapper(HousingStaffIntegralMapper.class);
        return mapper.hidden(id) > 0;
    }

    /**
     * 通过主键删除数据
     */
    @Override
    @TaskAnnotation("deleteBatchIds")
    public boolean deleteBatchIds(SessionFactory factory, List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        HousingStaffIntegralMapper mapper = factory.getMapper(HousingStaffIntegralMapper.class);
        return mapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @TaskAnnotation("removeByApplyId")
    public boolean removeByApplyId(SessionFactory factory, String applyId) {
        if (StringUtils.isEmpty(applyId)) {
            return false;
        }
        List<HousingStaffIntegral> integralList = listByApplyId(factory, applyId);
        return deleteBatchIds(factory, integralList.stream().map(HousingStaffIntegral::getId).collect(Collectors.toList()));

    }
}
